/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.DbMapper;
import cn.webrx.mapper.UserMapper;
import cn.webrx.model.Book;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-22 20:32:48
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Slf4j
public class Demo {
    BookMapper bookdao = null;
    UserMapper udao = null;
    DbMapper dm = null;

    @Before
    public void init() {
        //建立数据源 HikariDataSource
        var ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql:/wxdb");
        ds.setUsername("root");

        //建立事务对象
        var tx = new JdbcTransactionFactory();
        //建立环境对象
        var env = new Environment("development", tx, ds);
        //建立配置对象相当于mybatis-config.xml文件
        var cfg = new Configuration(env);

        var alias = cfg.getTypeAliasRegistry();
        alias.registerAliases("cn.webrx.model");

        cfg.addMappers("cn.webrx.mapper"); //会自动加载接口类，同时也加载接口同名的同位置的xml

        //加载resouces/mappers/*.xml
        //var xml = "mappers/xyz.xml";
        //var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xml);
        //System.out.println(is);
        //new XMLMapperBuilder(is, cfg, xml, cfg.getSqlFragments()).parse();


        //会自动批量加载/resources/mappers/*.xml
        var xml = "mappers/";
        var p = Thread.currentThread().getContextClassLoader().getResource(xml).getPath();
        var f = new File(p);
        for (String s : f.list()) {
            if (s.endsWith(".xml")) {
                var t = xml + s;
                var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(t);
                new XMLMapperBuilder(is, cfg, t, cfg.getSqlFragments()).parse();
            }
        }


        var sf = new SqlSessionFactoryBuilder().build(cfg);
        var ss = sf.openSession(true);

        bookdao = ss.getMapper(BookMapper.class);

        //log.info("{}", ss.selectList("vvv", List.class));
        udao = ss.getMapper(UserMapper.class);
        dm = ss.getMapper(DbMapper.class);
    }

    @After
    public void close() {

    }


    @Test
    public void t3() {

        log.info("{}", udao.ver());


    }

    @Test
    public void t4() {
        log.info("{}", dm.dbInfo());
    }

    @Test
    public void t2() {
        //log.info("{}", bookdao.shows("book"));
        log.info("{}", bookdao.tbs());
    }


    @Test
    public void t1() {
        //log.info(bookdao.ver());

        bookdao.add("《css样式入门21》", 43, "jack");

        int id = 9;
        Book book = bookdao.queryById(id);
        book.setAuthor("赵三三");

        bookdao.edit(book);

    }

    @Test
    public void insert() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "《前端开发技术 - Vue》");
        map.put("price", 55);
        map.put("author", "赵3");
        bookdao.save(map);
    }

    @Test
    public void delete() {
        bookdao.deleteById(2);
    }

    //@After
    public void query() {
        //log.info("{} -- {}", bookdao.queryAll(), String.format("%08d", 100));

        log.info("{}", bookdao.queryAll());
    }
}
